#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} You need to run this script as root! ${RESET}"; exit 1
fi

alamat_ip=$(wget -qO- ipv4.icanhazip.com)
alamat_hos=$(cat /etc/environment | grep 'DOMAIN' | cut -d ' ' -f 2)
nama_pengguna=$(cat /etc/environment | grep 'USERNAME' | cut -d ' ' -f 2)
client_port=$(cat /usr/local/etc/v2ray/trojan_tcp-tls.json | grep -i '"port":' | cut -d ' ' -f 2)
protocol=$(cat /usr/local/etc/v2ray/trojan_tcp-tls.json | grep -i '"protocol":' | cut -d ' ' -f 2)
network=$(cat /usr/local/etc/v2ray/trojan_tcp-tls.json | grep -i '"network":' | cut -d ' ' -f 2)
security=$(cat /usr/local/etc/v2ray/trojan_tcp-tls.json | grep -i '"security":' | cut -d ' ' -f 2)

until [[ $_namapengguna =~ ^[a-zA-Z0-9_]+$ && ${check_user} == 0 ]]; do
	read -p "Masukkan nama pengguna: " _namapengguna
	check_user=$(grep -w $_namapengguna /usr/local/etc/v2ray/.trojan | wc -l)
	if [[ ${check_user} -ne 0 ]]; then
		echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna sudah wujud! ${RESET}"; exit 1
	fi
done
read -p "Masukkan kata laluan: " _katalaluan
read -p "Masukkan tempoh aktif [Hari]: " _tempohaktif
tarikh_luput=$(date -d "$_tempohaktif days" +"%F")

sed -i '/# END $nama_pengguna/a # BEGIN $_namapengguna\n"password": "$_katalaluan",\n# END $_namapengguna' /usr/local/etc/v2ray/trojan_tcp-tls.json

trojan_link="trojan://${_namapengguna}@${alamat_hos}:${client_port}"
trojan_file="https://$alamat_hos/${_namapengguna}_trojan-tcp-tls.json"
echo "$alamat_ip $alamat_hos $client_port $_namapengguna $_tempohaktif $tarikh_luput " >> /usr/local/etc/v2ray/.trojan

clear
echo
echo -e "${TEXT} ========================================================== ${RESET}"
echo -e "${TEXT} TROJAN - BUAT AKAUN                                        ${RESET}"
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo
echo -e "${YELLOW}    Kenyataan:${RESET} ${GREEN}$_namapengguna${RESET}"
echo -e "${YELLOW}    Alamat IP:${RESET} ${GREEN}$alamat_ip${RESET}"
echo -e "${YELLOW}  Nama domain:${RESET} ${GREEN}$alamat_hos${RESET}"
echo -e "${YELLOW}     Protokol:${RESET} ${GREEN}$protocol${RESET}"
echo -e "${YELLOW}    Rangkaian:${RESET} ${GREEN}$network${RESET}"
echo -e "${YELLOW}  Keselamatan:${RESET} ${GREEN}$security${RESET}"
echo -e "${YELLOW}  Alamat port:${RESET} ${GREEN}$client_port${RESET}"
echo -e "${YELLOW}  Kata laluan:${RESET} ${GREEN}$_katalaluan${RESET}"
echo -e "${YELLOW} Tempoh aktif:${RESET} ${GREEN}$_tempohaktif days${RESET}"
echo -e "${YELLOW} Tarikh luput:${RESET} ${GREEN}$tarikh_luput${RESET}"
echo
echo " Pautan pelanggan: $trojan_link"
echo " Fail pelanggan: $trojan_file"
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo
